import { onMounted, ref } from "vue"
import { INewsCommonRes, INewsRes, INewsResquestParam, IPagination } from "./type"

function useGetPaginationData<T extends IPagination, U extends INewsCommonRes, K>(getNewsList: (params: any) => Promise<U>) {
  const newsData = ref<K[]>([])
  const loading = ref(false)
  const finished = ref(false)
  const showSkeleton = ref(true)

  async function getData(params: T) {
    loading.value = true
    const res = await getNewsList(params);
    if (res.code === 200) {
      if (params.page === 1) {
        newsData.value = res.data
      } else {
        newsData.value = newsData.value?.concat(res.data)
      }
      if (newsData.value.length >= res.total) {
        finished.value = true
      }
    }
    loading.value = false
    showSkeleton.value = false
  }
  return {
    newsData,
    loading,
    finished,
    getData,
    showSkeleton
  }
}

export default useGetPaginationData